This patch is to fix an issue in mc (midnight commander)
whereby sometimes the subshell output was truncated and if small enough
completely discarded.
The fix handles the case when the order of two different inputs
(shell output and the pwd from PROMPT_COMMAND) don't arrive in that order.
This happens sometimes and this is when the truncation was in effect.
Also note that shell being STOP-ped can also happen in any order with the above two.
Patch by EmanueL Czirai.

reported issue: https://github.com/MidnightCommander/mc/issues/44#issuecomment-60467020

UPdate: subshell.c location changed with commit: 974ab368ec30fcd68e595252668d1542a5352619

diff --git a/src/subshell.c b/src/subshell.c
index a85b258..f722396 100644
--- a/src/subshell/common.c
+++ b/src/subshell/common.c
@@ -464,17 +464,15 @@ feed_subshell (int how, int fail_on_error)
     int i;                      /* Loop counter */
 
     struct timeval wtime;       /* Maximum time we wait for the subshell */
-    struct timeval *wptr;
-
-    /* we wait up to 10 seconds if fail_on_error, forever otherwise */
-    wtime.tv_sec = 10;
-    wtime.tv_usec = 0;
-    wptr = fail_on_error ? &wtime : NULL;
+    int gotpwdoutput=FALSE;
 
     while (TRUE)
     {
         int maxfdp;
 
+        wtime.tv_sec = (fail_on_error && !gotpwdoutput) ? 10:0;
+        wtime.tv_usec = 10000;
+
         if (!subshell_alive)
             return FALSE;
 
@@ -490,7 +488,7 @@ feed_subshell (int how, int fail_on_error)
             maxfdp = max (maxfdp, STDIN_FILENO);
         }
 
-        if (select (maxfdp + 1, &read_set, NULL, NULL, wptr) == -1)
+        if (select (maxfdp + 1, &read_set, NULL, NULL, ( (ACTIVE == subshell_state && subshell_ready) ? NULL : &wtime)  ) == -1)
         {
             /* Despite using SA_RESTART, we still have to check for this */
             if (errno == EINTR)
@@ -508,11 +506,6 @@ feed_subshell (int how, int fail_on_error)
 
         if (FD_ISSET (mc_global.tty.subshell_pty, &read_set))
             /* Read from the subshell, write to stdout */
-
-            /* This loop improves performance by reducing context switches
-               by a factor of 20 or so... unfortunately, it also hangs MC
-               randomly, because of an apparent Linux bug.  Investigate. */
-            /* for (i=0; i<5; ++i)  * FIXME -- experimental */
         {
             bytes = read (mc_global.tty.subshell_pty, pty_buffer, sizeof (pty_buffer));
 
@@ -545,14 +538,7 @@ feed_subshell (int how, int fail_on_error)
 
             subshell_cwd[bytes - 1] = 0;        /* Squash the final '\n' */
 
-            synchronize ();
-
-            subshell_ready = TRUE;
-            if (subshell_state == RUNNING_COMMAND)
-            {
-                subshell_state = INACTIVE;
-                return TRUE;
-            }
+            gotpwdoutput=TRUE;
         }
 
         else if (FD_ISSET (STDIN_FILENO, &read_set))
@@ -581,9 +567,33 @@ feed_subshell (int how, int fail_on_error)
             if (pty_buffer[bytes - 1] == '\n' || pty_buffer[bytes - 1] == '\r')
                 subshell_ready = FALSE;
         }
-        else
-            return FALSE;
+        else {
+          if (fail_on_error) {
+            if (subshell_stopped && gotpwdoutput && subshell_state==RUNNING_COMMAND) {
+              break;
+            }else{
+              return FALSE;
+            }
+          }else {
+            if (subshell_stopped) {
+              if (subshell_state == ACTIVE) {
+                synchronize ();
+                subshell_ready = TRUE;
+              } else {
+                break;
+              }
+            }
+          }
+        }
+    }
+
+    synchronize ();
+    subshell_ready = TRUE;
+    if (subshell_state == RUNNING_COMMAND)
+    {
+      subshell_state = INACTIVE;
     }
+    return TRUE;
 }
 
 /* --------------------------------------------------------------------------------------------- */
